﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMS_TC_EQUIPMENT_INV : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMS_TC_EQUIPMENT_INV()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL =" select b.fpoint_name,d.fdictname,a.* "
              + "  from s_tc_equipment_inv a"
              + "  left join a_ts_point b on a.fpoint_no = b.fpoint_no"
              + "  left join s_ts_equipment_info c on a.feuipmentid = c.feuipmentid"
              + "  left join s_ts_equipment_dict d on d.fdictno = c.fequipmenttype"
              + "  where 1 = 1";
            DataRowView dr2 = (DataRowView)cbfequipmenttype.SelectedItem;
            if ((dr2 != null) && (cbfequipmenttype.SelectedIndex > 0))
            {
                SQL = SQL + " and c.fequipmenttype=" + dr2.Row["fdictno"].ToString();
            }

            DataRowView dr1 = (DataRowView)cbfpoint_no.SelectedItem;
            if ((dr1 != null) && (cbfpoint_no.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fpoint_no=" + dr1.Row["fpoint_no"].ToString();
            }

            if (uifcasno.Text != "")
            {
                SQL = SQL + " and ((a.fequipmentbarcode like '%" + uifcasno.Text + "%') or (a.fequipmenttag like '%" + uifcasno.Text + "%'))";
            }

            SQL = SQL + "  order by a.fpoint_no,c.fequipmenttype";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
            
        }

        private void FMS_TC_EQUIPMENT_INV_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_point";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys02.Visible = false; sys02.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("S_TS_EQUIPMENT_STATE", "");
            uifstate.DataSource = myTs_Dict;
            uifstate.DisplayMember = "fstatename";
            uifstate.ValueMember = "fstateno";

            DataTable myTs_Point = myCache.doGetDict("A_TS_POINT", "fpoint_flag=0");
            uifpoint_no.DataSource = myTs_Point;
            uifpoint_no.DisplayMember = "fpoint_name";
            uifpoint_no.ValueMember = "fpoint_no";

            DataRow dr1 = myTs_Point.NewRow();
            dr1["fpoint_name"] = "请选择";
            dr1["fpoint_no"] = "0";
            myTs_Point.Rows.InsertAt(dr1, 0);
            cbfpoint_no.DataSource = myTs_Point;
            cbfpoint_no.DisplayMember = "fpoint_name";
            cbfpoint_no.ValueMember = "fpoint_no";
            cbfpoint_no.SelectedIndex = 0;

            DataTable myTs_Dict2 = myCache.doGetDict("S_TS_EQUIPMENT_DICT", "");

            DataRow dr2 = myTs_Dict2.NewRow();
            dr2["fdictname"] = "请选择";
            dr2["fdictno"] = "0";
            myTs_Dict2.Rows.InsertAt(dr2, 0);
            cbfequipmenttype.DataSource = myTs_Dict2;
            cbfequipmenttype.DisplayMember = "fdictname";
            cbfequipmenttype.ValueMember = "fdictno";
            cbfequipmenttype.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifequipmentbarcode.Text = uiDGView["fequipmentbarcode", rowindex].Value.ToString();
                uifequipmenttag.Text = uiDGView["fequipmenttag", rowindex].Value.ToString();
                uifmacno.Text = uiDGView["fmacno", rowindex].Value.ToString();
                uifpoint_size.Text = uiDGView["fpoint_size", rowindex].Value.ToString();

                int i = 0;
                string sstatus = uiDGView["fstate", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifstate.Items)
                {
                    string id = dr["fstateno"].ToString();
                    if (id.Equals(sstatus))
                    {
                        uifstate.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sfpoint_no = uiDGView["fpoint_no", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifpoint_no.Items)
                {
                    string id = dr["fpoint_no"].ToString();
                    if (id.Equals(sfpoint_no))
                    {
                        uifpoint_no.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

            }

        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        private void cbfpoint_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbfequipmenttype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
